package com.example.gzysmartcity.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.HospData;
import com.example.gzysmartcity.repository.model.SCAdministrative;
import com.example.gzysmartcity.repository.model.SCHospital;
import com.example.gzysmartcity.repository.model.SCOutpatient;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HospViewModel extends ViewModel {
    private final MutableLiveData<State> hospLiveData = new MutableLiveData<>();
    private List<SCHospital.DataBean> sCHospitalList;
    private List<SCAdministrative.DataBean> sCAdministrativeList;
    private List<SCOutpatient.DataBean> sCOutpatientList;
    private List<HospData> hospDataList;

    private List<String> hospspList;

    //医院数据
    public void getSCHospitalData() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCHospital>() {
            @Override
            public SCHospital doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCHospital/getAll?", null, SCHospital.class, 0);
            }

            @Override
            public void onSuccess(SCHospital result) {
                sCHospitalList = result.getData();
                getSCAdministraData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
            }
        });
    }

    //科室数据
    public void getSCAdministraData() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCAdministrative>() {
            @Override
            public SCAdministrative doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCAdministrative/getAll?", null, SCAdministrative.class, 0);
            }

            @Override
            public void onSuccess(SCAdministrative result) {
                sCAdministrativeList = result.getData();
                getSCOutpatientsData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
            }
        });
    }

    //门诊数据
    public void getSCOutpatientsData() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<SCOutpatient>() {
            @Override
            public SCOutpatient doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("SCOutpatient/getAll?", null, SCOutpatient.class, 0);
            }

            @Override
            public void onSuccess(SCOutpatient result) {
                sCOutpatientList = result.getData();
                getHospData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
            }
        });
    }

    public void getHospData() {
        //拿取医院数据
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {

            @Override
            public Object doInBackground() {
                hospDataList = new ArrayList<>();
                hospspList = new ArrayList<>();
                for (SCHospital.DataBean datum : sCHospitalList) {
                    hospspList.add(datum.getName());
                    sCAdministrativeList.stream().filter(a -> a.getSCHospitalId() == datum.getId()).forEach(a -> sCOutpatientList.stream()
                            .filter(b -> b.getSCAdministrativeId() == a.getId())
                            .forEach(b -> hospDataList.add(new HospData(b.getId(), datum.getName(), a.getName(), b.getName()))));
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                hospLiveData.postValue(State.SUCCESS);
            }
        });
    }


    public List<String> getHospspList() {
        return hospspList;
    }


    public void getKsspList(List<String> list, String name) {
        sCAdministrativeList.stream().filter(a -> a.getSCHospitalId() == sCHospitalList.stream().filter(b -> b.getName().equals(name)).findFirst().get().getId()).
                forEach(c -> list.add(c.getName()));
    }

    public void getMzSpLists(List<String> list, String hospital, String name) {
        int id = sCHospitalList.stream().filter(b -> b.getName().equals(hospital)).findFirst().get().getId();
        sCOutpatientList.stream().filter(a -> a.getSCHospitalId() == id).filter(a -> a.getSCAdministrativeId() ==
                sCAdministrativeList.stream().filter(b -> b.getSCHospitalId() == id).filter(b -> b.getName().equals(name)).findFirst().get().getId())
                .forEach(c -> list.add(c.getName()));
    }

    public List<HospData> search(String h, String k, String m) {
        Log.i("TAG", "search: " + h + k + m);
        return hospDataList.stream().filter(a -> a.getHospName().equals(h) && a.getKeShiName().equals(k) && a.getMenZhenName().equals(m)).collect(Collectors.toList());
    }

    public List<HospData> getHospDataList() {
        return hospDataList;
    }

    public LiveData<State> gethospLiveData() {
        return hospLiveData;
    }
}
